<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="企业名称" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入企业名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="企业类别" prop="companyType" v-show="this.$store.state.user.info.deptId == 100">
        <el-select v-model="queryParams.companyType" placeholder="请选择企业类别" clearable>
          <el-option v-for="dict in dict.type.sys_org_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deptList" row-key="deptId" :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" :row-style="{ height: '30px' }" border
      :cell-style="{ padding: '0px' }" height="400px">
      <el-table-column label="序号" type="index" align="center" prop="index" :index="indexMethod"></el-table-column>
      <el-table-column prop="deptName" label="企业名称" width="250" align="center" :show-overflow-tooltip="true">

      </el-table-column>
      <el-table-column prop="address" label="企业地址" width="300" align="center"
        :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="companyOrigin" label="企业来源" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.enterprise_source" :value="scope.row.companyOrigin" />
        </template>
      </el-table-column>
      <!-- <el-table-column prop="abbreviate" label="企业简称" show-overflow-tooltip="true"></el-table-column> -->
      <el-table-column prop="nature" label="企业性质" :show-overflow-tooltip="true" align="center">

        <template slot-scope="scope">
          <dict-tag :options="dict.type.enterprise_nature" :value="scope.row.nature" />
        </template>
      </el-table-column>

      <el-table-column prop="contacts" label="联系人" align="center" width="90"
        :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="phone" label="联系电话" width="100" :show-overflow-tooltip="true"
        align="center"></el-table-column>
      <el-table-column prop="legalPerson" label="法人代表" align="center" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="companyType" label="企业类别" align="center">

        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_org_type" :value="scope.row.companyType" />
        </template>
      </el-table-column>
      <!-- <el-table-column prop="type" label="类别" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dept_type" :value="scope.row.type" />
        </template>
      </el-table-column> -->
      <el-table-column prop="foundTime" label="成立时间" :show-overflow-tooltip="true" width="90" align="center">

        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.foundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="80" align="center">

        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="80" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.type == '1'" size="mini" type="text" icon="el-icon-thumb"
            @click="handleLook(scope.row, scope.$index + 1)" title="查看"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改部门对话框 用于平台管理员 -->
    <el-dialog :title="title" :visible.sync="openAdmin" width="700" append-to-body>
      <div v-if="opt != 'add'">
        <el-descriptions title="">
          <el-descriptions-item label="序号">
            <el-tag size="medium">{{ index }}</el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <el-form ref="form" :model="form" :rules="rulesAdmin" label-width="80px">
        <el-row>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="企业名称" prop="deptName">
              <el-input v-model="form.deptName" placeholder="请输入企业名称" :disabled="isShow" />
            </el-form-item>
          </el-col>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="企业来源" prop="companyOrigin">
              <el-select v-model="form.companyOrigin" style="width: 280px" placeholder="请选择企业来源" :disabled="isShow">
                <el-option v-for="dict in dict.type.enterprise_source" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="企业性质" prop="nature">
              <el-select style="width: 280px" v-model="form.nature" placeholder="请选择企业性质" :disabled="isShow">
                <el-option v-for="dict in dict.type.enterprise_nature" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12" style="height: 55px;">
            <el-form-item label="所属行业" prop="industry">
              <el-select v-show="form.companyType == 0" v-model="form.industry"  style="width: 280px"
                                :disabled="isShow">
               <el-option v-for="  dict   in   dict.type.customer_industry  " :key="dict.value"
                                    :label="dict.label" :value="dict.value"></el-option>
               </el-select>
                            <el-select v-show="form.companyType == 1" v-model="form.industry"  style="width: 280px"
                                :disabled="isShow">
                                <el-option v-for="  dict   in   dict.type.customer_industry_serve  " :key="dict.value"
                                    :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                            <el-select v-show="form.companyType == 2" v-model="form.industry"  style="width: 280px"
                                :disabled="isShow">
                                <el-option v-for="  dict   in   dict.type.customer_industry_garden  " :key="dict.value"
                                    :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="法人代表" prop="legalPerson">
              <el-input v-model="form.legalPerson" placeholder="请输入法人代表" maxlength="20" :disabled="isShow" />
            </el-form-item>
          </el-col>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="成立时间" prop="foundTime">
              <el-date-picker v-model="form.foundTime" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                style="width: 280px" :disabled="isShow">
              </el-date-picker>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入联系人" maxlength="20" :disabled="isShow" />
            </el-form-item>
          </el-col>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" maxlength="11" :disabled="isShow" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="注册资金" prop="registeredFund">
              <el-input v-model.number="form.registeredFund" placeholder="请输注册资金" :disabled="isShow" />
            </el-form-item>
          </el-col>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="企业类别">
              <el-radio-group v-model="form.companyType" :disabled="isShow">
                <el-radio v-for="dict in dict.type.sys_org_type" :key="dict.value" :label="dict.value">{{
      dict.label
    }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12" style="height: 55px;">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="10" style="height: 55px;">
            <el-form-item label="显示排序" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" :min="0" :disabled="isShow" />
            </el-form-item>
          </el-col>
          <el-col :span="14" style="height: 55px;">
            <el-form-item label="统一社会信用代码" prop="creditCode" label-width="142px">
              <el-input v-model="form.creditCode" placeholder="请输入统一社会信用代码" :disabled="isShow" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="企业地址" prop="address">
            <el-input v-model="form.address" type="textarea" placeholder="请输入企业地址" :disabled="isShow" />
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="经营范围" prop="businessScope">
            <el-input v-model="form.businessScope" type="textarea" placeholder="请输入经营范围" :disabled="isShow"
              :maxlength="3000" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="个性签名" prop="personalSignature">
            <el-input v-model="form.personalSignature" type="textarea" placeholder="请输入个性签名" :disabled="isShow"
              :maxlength="2000" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="企业介绍" prop="companyProfile">
            <el-input v-model="form.companyProfile" type="textarea" placeholder="请输入企业介绍" :disabled="isShow"
              :maxlength="3000" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="企业状态">
              <el-radio-group v-model="form.status" :disabled="isShow">
                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{
      dict.label
    }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12" style="height: 55px;">
            <el-form-item label="审核结果" prop="isCertified">
              <el-radio-group v-model="form.isCertified" @change="changeReviewComments">
                <el-radio :label="2">同意</el-radio>
                <el-radio :label="0">不同意</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row> <el-row>
          <el-form-item label="审核意见" prop="reviewComments">
            <el-input v-model="form.reviewComments" type="textarea" placeholder="请输入审核意见" :maxlength="1000" />
          </el-form-item>
        </el-row>
        <!-- <el-row>
          <el-form-item label="企业logo">
            <file-view :type="fileType" :businessId="businessId" :businessCode="businessCode" @change="getFile"
              :key="refreshKey"></file-view>
          </el-form-item>
        </el-row> -->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancelAdmin">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeptExcludeChild, listByIsCertified, listDept, getDept, delDept, addDept, updateDept } from "@/api/system/dept";
import { enterpriseVerify } from "@/api/enterprise/info";

export default {
  name: "Dept",
  dicts: ['customer_industry_serve', 'customer_industry_garden','enterprise_nature', 'sys_org_type', 'sys_normal_disable', 'customer_industry', 'sys_dept_type', 'sys_org_type', 'enterprise_source'],
  data() {
    return {
      opt: "edit",
      isShow: false,
      index: null,
      deptType: null,
      userNewId: "",
      openAdmin: false,
      fileType: 0,
      // 总条数
      total: 0,
      businessId: null,
      businessCode: "enterprise_logo",
      refreshKey: 0,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      disabled: false,
      dept: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        parentId: null,
        ancestors: null,
        deptName: null,
        type: null,
        companyOrigin: null,
        abbreviate: null,
        registeredFund: null,
        legalPerson: null,
        organizationType: null,
        companyType: null,
        nature: null,
        area: null,
        foundTime: null,
        businessScope: null,
        companyProfile: null,
        personalSignature: null,
        industry: null,
        contacts: null,
        address: null,
        orderNum: null,
        leader: null,
        phone: null,
        email: null,
        status: null,
        belongTo: null,
        fileId: null,
        creditCode: null,
        isCertified: null,
        reviewComments: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rulesAdmin: {
        reviewComments: [
          { required: true, message: "审核意见不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    if (this.$store.state.user.info.deptId == 100) {
      this.showSearch = true
    } else {
      this.showSearch = false
    }
    this.getList();
  },
  methods: {
    changeReviewComments() {
      if (this.form.isCertified == 2) {
        this.form.reviewComments = "通过审核";
      } else {
        this.form.reviewComments = "";
      }
    },
    cancelAdmin() {
      // if(this.$store.state.user.info.deptId==100){
      //     this.openAdmin = false
      // }else{
      //     this.open = false;
      // }
      //this.reset();
      this.openAdmin = false;
    },
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },
    handleLook(row, index) {
      this.reset();
      this.fileType = 1;
      this.opt = "edit";
      this.isShow = true;
      this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index;
      this.refreshKey++;
      if (row.fileId == "" || row.fileId == null || row.fileId == undefined) {
        this.businessId = row.deptId;
      } else {
        this.businessId = row.fileId;
      }

      getDept(row.deptId).then(response => {
        this.form = response.data;
        this.form.isCertified = 2;
        if (this.form.isCertified == 2) {
          this.form.reviewComments = "通过审核";
        }
        this.deptType = response.data.type
        let parent = response.data.parentId;
        this.title = "";
        if (response.data.type == "0") {//等于部门
          this.open = true;
          this.title = "查看机构部门";
        } else {
          this.openAdmin = true
          this.title = "企业入驻审批";
        }
        listDeptExcludeChild(row.deptId).then(response => {
          //console.log(response)
          this.deptOptions = this.handleTree(response.data, "deptId");
          if (this.deptOptions.length == 0) {
            const noResultsOptions = { deptId: this.form.parentId, deptName: this.form.parentName, children: [] };
            this.deptOptions.push(noResultsOptions);
          }
        });
      });
    },
    /** 查询认证待审核列表 */
    getList() {
      this.loading = true;
      this.queryParams.isCertified = "1";
      listByIsCertified(this.queryParams).then(response => {
        this.deptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: null,
        parentId: null,
        ancestors: null,
        deptName: null,
        type: null,
        companyOrigin: null,
        abbreviate: null,
        registeredFund: null,
        legalPerson: null,
        organizationType: null,
        companyType: null,
        nature: null,
        area: null,
        foundTime: null,
        businessScope: null,
        companyProfile: null,
        personalSignature: null,
        industry: null,
        contacts: null,
        address: null,
        orderNum: null,
        leader: null,
        phone: null,
        email: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        belongTo: null,
        fileId: null,
        creditCode: null,
        isCertified: null,
        reviewComments: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deptId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加部门";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deptId = row.deptId || this.ids
      getDept(deptId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改部门";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          enterpriseVerify(this.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.openAdmin = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deptIds = row.deptId || this.ids;
      this.$modal.confirm('是否确认删除部门编号为"' + deptIds + '"的数据项？').then(function () {
        return delDept(deptIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dept/export', {
        ...this.queryParams
      }, `dept_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
